package com.neu.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.dao.impl.WorkerDao;
import com.neu.pojo.Worker;
import com.neu.service.impl.WorkerService;
import com.neu.table.WorkerTable;

import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTable;

public class AdminUI extends JFrame {
	//组件
	private JPanel contentPane;
	private JTextField textField;
	private JTable table_1;
	//数据
	private WorkerTable workerTable = new WorkerTable();
	//service
	private WorkerService workerService = WorkerService.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminUI frame = new AdminUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 830, 546);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(59, 41, 136, 31);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("姓名查询");
		btnNewButton.setBounds(258, 41, 146, 31);
		contentPane.add(btnNewButton);
		
		//下拉列表
		JComboBox<String> jobs = new JComboBox<String>();
		jobs.setBounds(59, 112, 106, 36);
		contentPane.add(jobs);
		
		JButton btnNewButton_1 = new JButton("添加工作人员");
		btnNewButton_1.setBounds(33, 158, 181, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.setBounds(483, 158, 222, 23);
		contentPane.add(btnNewButton_2);
		//表格
		table_1 = new JTable(this.workerTable);
		table_1.setBounds(60, 208, 706, 259);
		contentPane.add(table_1);
		
		JButton btnNewButton_3 = new JButton("保存");
		btnNewButton_3.setBounds(158, 476, 97, 23);
		contentPane.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("返回");
		btnNewButton_4.setBounds(519, 476, 97, 23);
		contentPane.add(btnNewButton_4);
		//返回
		btnNewButton_4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Login login = new Login();
				login.setVisible(true);
				setVisible(false);
			}
		});
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table_1);
		jp.setBounds(60, 208, 706, 259);
		contentPane.add(jp);
		
		//窗口居中
		setLocationRelativeTo(null);

		//下拉列表显示
		jobs.addItem("所有");
		jobs.addItem("管理员");
		jobs.addItem("医生");
		jobs.addItem("护工");
		
		//下拉列表监听
		jobs.addActionListener(new ActionListener() {//由下拉列表展示不同类型对象
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableRowSorter sorter = new TableRowSorter(workerTable);
				table_1.setRowSorter(sorter);
			    String s=(String) jobs.getSelectedItem();
				
			    switch (s) {
			    	case "所有":
			    		sorter.setRowFilter(RowFilter.regexFilter(textField.getText()));
			    		break;
			    	case "管理员":
			    		List<RowFilter<Object, Object>> filter3=new ArrayList<>(2);
				    	filter3.add(RowFilter.regexFilter(s));
				    	filter3.add(RowFilter.regexFilter(textField.getText()));
				    	sorter.setRowFilter(RowFilter.andFilter(filter3));
				    	break;
			    	case "医生":
				    	List<RowFilter<Object, Object>> filters=new ArrayList<>(2);
				    	filters.add(RowFilter.regexFilter(s));
				    	filters.add(RowFilter.regexFilter(textField.getText()));
				    	sorter.setRowFilter(RowFilter.andFilter(filters));
				    	break;
				    case "护工":
				    	List<RowFilter<Object, Object>> filters1=new ArrayList<>(2);
				    	filters1.add(RowFilter.regexFilter(s));
				    	filters1.add(RowFilter.regexFilter(textField.getText()));
				    	sorter.setRowFilter(RowFilter.andFilter(filters1));
				    	break;
				    default:
				    	break;
			    }
			}
		});
		
		//按钮监听
		btnNewButton.addActionListener(new ActionListener() {//由下拉列表内容和文本框内容展示不同类型对象
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableRowSorter sorter = new TableRowSorter(workerTable);
				table_1.setRowSorter(sorter);
			    String s=(String) jobs.getSelectedItem();
			    
			    switch (s) {
		    	case "所有":
		    		sorter.setRowFilter(RowFilter.regexFilter(textField.getText()));
		    		break;
		    	case "管理员":
		    		List<RowFilter<Object, Object>> filter3=new ArrayList<>(2);
			    	filter3.add(RowFilter.regexFilter(textField.getText()));
			    	sorter.setRowFilter(RowFilter.andFilter(filter3));
			    	break;
		    	case "医生":
			    	List<RowFilter<Object, Object>> filters=new ArrayList<>(2);
			    	filters.add(RowFilter.regexFilter(textField.getText()));
			    	sorter.setRowFilter(RowFilter.andFilter(filters));
			    	break;
			    case "护工":
			    	List<RowFilter<Object, Object>> filters1=new ArrayList<>(2);
			    	filters1.add(RowFilter.regexFilter(textField.getText()));
			    	sorter.setRowFilter(RowFilter.andFilter(filters1));
			    	break;
			    default:
			    	break;
		    }
				
			}
		});
		
		//添加工作人员
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				RegistUI registUI = new RegistUI();//创建新窗口
				registUI.setVisible(true);
				setVisible(false);
			}
		});
		
		//删除
		btnNewButton_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//删除数据
				workerTable.delete();
				
				workerTable.fireTableDataChanged();
				JOptionPane.showMessageDialog(null, "删除成功");
				
			}
		});
		
		//保存
		btnNewButton_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//更新每一行内容，存储文件到文件中
				for(int i=0;i<workerTable.getRowCount();i++){
					Worker worker = new Worker();
					worker.setNumber((String)workerTable.getValueAt(i, 1));
					worker.setBirthday((String)workerTable.getValueAt(i, 3));
					worker.setJob((String)workerTable.getValueAt(i, 5));
					worker.setKind((String)workerTable.getValueAt(i, 4));
					worker.setName((String)workerTable.getValueAt(i, 2));
					worker.setPassword(WorkerDao.getInstance().search(new Worker((String)workerTable.getValueAt(i, 1))).getPassword());
					worker.setPhone((String)workerTable.getValueAt(i, 6));
					WorkerService.getInstance().cover(worker);
				}
				JOptionPane.showMessageDialog(null, "保存成功");
			}
		});
	}
}
